package com.shoparc.geo;

import java.util.ArrayList;

import org.apache.commons.math3.geometry.euclidean.threed.Rotation;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import processing.core.PApplet;


public class Element {
	public ArrayList<Vector3D> vertices;
	protected Vector3D barycenter;

	public Element(ArrayList<Vector3D> vertices){	
		this.vertices = vertices;
		getBarycenter();
	}
	
	public void translate(Vector3D t){
		for(int i =0; i<vertices.size(); i++){
			Vector3D v = vertices.get(i);
			v = v.add(t);
			vertices.set(i, v);
		}
	
		barycenter = barycenter.add(t);
	}
	
	public void rotate(Rotation r){
		
		for(int i =0; i<vertices.size(); i++){
			Vector3D v = vertices.get(i);
			v = v.subtract(barycenter);
			v = r.applyTo(v);
			v = v.add(barycenter);
			vertices.set(i, v);
		}
	}
	
	public void scale(double x, double y, double z){
		for(int i =0; i<vertices.size(); i++){
			Vector3D v = vertices.get(i);
			v = v.subtract(barycenter);
			v = new Vector3D(v.getX()*x, v.getY()*y, v.getZ()*z);
			v = v.add(barycenter);
			vertices.set(i, v);
		}
	}
	
	public Vector3D getBarycenter(){
		barycenter = new Vector3D(0,0,0);
		for(Vector3D v : vertices ){
			barycenter = barycenter.add(v);
		}
		barycenter = barycenter.scalarMultiply((1.0/vertices.size()));
		return barycenter;
	}
	
	public void renderPushVerts(PApplet parent){
		for (Vector3D v : vertices) {
			parent.vertex((float) v.getX(), (float) v.getY(), (float) v.getZ());
		}
	}

}
